package com.android.yummycookies.activeBoard;

import android.graphics.Canvas;
import android.view.SurfaceView;

import com.android.yummycookies.commonInterface.IGameEngine;
import com.android.yummycookies.model.commonModel.GameLoop;
import com.android.yummycookies.model.theme.Theme;

public abstract class OneBoardGameEngine extends AbstractGameEngine {

	private GameLoop<IGameEngine> gameLoop;

	private Theme theme;

	private final SurfaceView surfaceView;
	private final CookieBoard cookieBoard;

	private final int gameBoardHeight;
	private final int gameBoardWidth;

	private final int cookiesHeigt;
	private final int cookiesWidth;

	public OneBoardGameEngine(Theme theme, SurfaceView surfaceView,
			int maxHeightCookies, int maxWidthCookies) {
		super(maxHeightCookies, maxWidthCookies);

		this.theme = theme;
		this.surfaceView = surfaceView;
		gameBoardHeight = surfaceView.getHeight();
		gameBoardWidth = surfaceView.getWidth();

		cookiesHeigt = gameBoardHeight / maxHeightCookies;
		cookiesWidth = gameBoardWidth / maxWidthCookies;

		cookieBoard = new CookieBoard(maxHeightCookies, maxWidthCookies,
				cookiesHeigt, cookiesWidth);

		gameLoop = new GameLoop<IGameEngine>(this, surfaceView.getHolder());
	}

	/******************************************************
	 * Interface IGameEngine
	 ******************************************************/

	@Override
	public void draw(Canvas canvas) {
		cookieBoard.draw(canvas);
	}

	@Override
	public void update() {
		cookieBoard.update();
	}

	public boolean isRunning() {
		return gameLoop.isRunning();
	}

	public void start() {
		gameLoop.start();
	}

	public void resume() {
		gameLoop.notify();
	}

	public void pause() {
		try {
			gameLoop.wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void stop() {
		gameLoop.setRunning(false);
	}

	/*************************************************
	 * Other methods
	 *************************************************/

	public Theme getTheme() {
		return theme;
	}
	
	public SurfaceView getSurfaceView() {
		return surfaceView;
	}
	
	public CookieBoard getCookieBoard() {
		return cookieBoard;
	}

	public int getGameBoardHeight() {
		return gameBoardHeight;
	}

	public int getGameBoardWidth() {
		return gameBoardWidth;
	}

	public int getCookiesHeigt() {
		return cookiesHeigt;
	}

	public int getCookiesWidth() {
		return cookiesWidth;
	}

}
